package cn.bluethink.timescaledb.controller;

import cn.bluethink.timescaledb.entity.ResponseDTO;
import cn.bluethink.timescaledb.filter.QueryFilter;
import cn.bluethink.timescaledb.service.TimeScaleDynamicService;
import cn.bluethink.timescaledb.util.FilterUtil;
import onegis.common.utils.StreamUtils;
import onegis.filter.GFilter;
import onegis.message.ResponseMessage;
import onegis.protobuf.model.PbFormatData;
import onegis.protobuf.proto.utils.PbFilterTool;
import onegis.protobuf.proto.utils.result.PbResultTool;
import onegis.psde.dynamicdata.DynamicDatas;
import onegis.result.ResultData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/time/dynamic")
@CrossOrigin
public class TimeScaleDynamicController {

    private Logger logger = LoggerFactory.getLogger(TimeScaleDynamicController.class);

    @Autowired
    TimeScaleDynamicService timeScaleDynamicService;

    @GetMapping("/hello")
    public String sayHello() {
        return "Hello,TimeScaleDynamic Service !";
    }

    @PostMapping("/save")
    public ResponseDTO save(@RequestBody DynamicDatas dynamicDatas) throws ParseException {
        return timeScaleDynamicService.save(dynamicDatas);
    }

    @GetMapping("/query")
    public ResponseDTO query(@RequestBody QueryFilter queryFilter) throws Exception {
        String feign = queryFilter.getgFilter().getFeign() ? "feign" : "notfeign";
        return timeScaleDynamicService.query(FilterUtil.formatFilter(queryFilter.getgFilter()), feign, false, queryFilter);
    }

    @GetMapping("/query/pb")
    public void queryPB(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setAttribute("datatype", "pb");
        GFilter spatialFilter = new GFilter();
        try {
            byte[] data = StreamUtils.inputStreamToByte(request.getInputStream());
            PbFormatData.PbDynamicDataFilter pbDynamicDataFilter = PbFormatData.PbDynamicDataFilter.parseFrom(data);
            spatialFilter = PbFilterTool.pbDynamicDataFilterTods(pbDynamicDataFilter);
        } catch (Exception e) {
            logger.error("转化异常");
            e.printStackTrace();
        }
        List<GFilter> gFilters = new ArrayList<>();
        GFilter gFilter = FilterUtil.formatFilter(spatialFilter);
        gFilters.add(gFilter);
        QueryFilter queryFilter = new QueryFilter();
        queryFilter.setgFilter(gFilter);
        ResponseDTO responseDTO = timeScaleDynamicService.query(FilterUtil.formatFilter(gFilters.get(0)), "notfeign", false, queryFilter);
        ResultData<?> resultData = new ResultData<>();
        resultData.setData(responseDTO.getData());
        resultData.setState(ResponseMessage.OK);
        PbFormatData.PbResponseResult dsListToPBData = PbResultTool.dsDynamicDatasTopbData(resultData);
        byte[] data = dsListToPBData.toByteArray();
        try {
            StreamUtils.writeBytes(response, data);
        } catch (IOException e) {
        }
    }

}
